package com.glsc.ngateway.common.base.repo.mysql.cmdb;

import com.glsc.ngateway.common.base.domain.mysql.cmdb.CmdbMemoryDetail;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
public interface CmdbMemoryDetailRepository extends JpaRepository<CmdbMemoryDetail, Integer>, JpaSpecificationExecutor<CmdbMemoryDetail> {
    List<CmdbMemoryDetail> findAllByMemoryDetailIdIn(List<Long> id);

    @Query(value = "select m from CmdbMemoryDetail m where m.isDelete=0 " +
            "and (:#{#paramSearchDto.memoryDetailCode} is null OR m.memoryDetailCode LIKE CONCAT('%', :#{#paramSearchDto.memoryDetailCode},'%') ) " +
            "and (:#{#paramSearchDto.memoryStandardCode} is null OR m.memoryStandardCode = :#{#paramSearchDto.memoryStandardCode}) " +
            "and (:#{#paramSearchDto.sn} is null OR m.sn LIKE CONCAT('%',:#{#paramSearchDto.sn},'%') ) " +
            "and (:#{#paramSearchDto.manufactDate} is null OR m.manufactDate = :#{#paramSearchDto.manufactDate}) " +
            "and (:#{#paramSearchDto.deviceCode} is null OR m.deviceCode = :#{#paramSearchDto.deviceCode}) " +
            "and (:#{#paramSearchDto.lifeStatus} is null OR m.lifeStatus = :#{#paramSearchDto.lifeStatus}) " +
            "and (:#{#paramSearchDto.locationId} is null OR m.locationId = :#{#paramSearchDto.locationId}) " +
            "and (:#{#paramSearchDto.expirateDate} is null OR m.expirateDate = :#{#paramSearchDto.expirateDate}) " +
            "")
    Page<CmdbMemoryDetail> searchByParam(Pageable pageable, CmdbMemoryDetail paramSearchDto);

    @Transactional
    @Modifying
    @Query(value = "update t_cmdb_memory_detail set is_delete = :deleted where memory_detail_code in :codes ", nativeQuery = true)
    void updateIsDeleteByCodes(Integer deleted, List<String> codes);

    @Transactional
    @Modifying
    @Query(value = "update t_cmdb_memory_detail set is_delete = :deleted where location_id = :deviceDetailId and life_status = 1", nativeQuery = true)
    void updateIsDeleteByDeviceDetailId(Integer deleted, Integer deviceDetailId);

    List<CmdbMemoryDetail> findAllBySnAndIsDelete(String sn, Integer noDelete);

    CmdbMemoryDetail findByMemoryDetailId(Integer memoryDetailId);

    List<CmdbMemoryDetail> findAllByMemoryDetailCodeIn(List<String> collect);

    List<CmdbMemoryDetail> findAllByDeviceCodeAndIsDelete(String deviceCode, Integer noDeleted);

}

